
import RobotError from "../model/errors/robot-error.js";
import Player from "../model/player/player.js";


export const enum HAND {
	/**石头 */
	STONE = "石头",
	/**剪刀 */
	SCILLORS = "剪刀",
	/**布 */
	CLOTH = "布"
}

const enum HAND_RESULT {
	DRAW,
	HAND_1_WIN,
	HAND_2_WIN
}



/**猜拳 */
export default class GuessHand {
	private player: Player;
	constructor(player: Player) {
		this.player = player;
	}

	randHand(): HAND {
		switch (Math.floor(Math.random() * 3)) {
			case 0: return HAND.CLOTH;
			case 1: return HAND.SCILLORS;
			case 2: return HAND.STONE;
		}
	}

	do(hand: HAND = this.randHand(), betting: number) {
		if (betting <= 0) throw new RobotError("押注硬币数不能小等于0");
		if (betting > this.player.coin) throw new RobotError("硬币不足");


		let robotHand = this.randHand();

		let sm = `你出的是${hand}，我出的是${robotHand}\n`;
		switch (this.decide(hand, robotHand)) {
			case HAND_RESULT.DRAW:
				sm += `平了，你的硬币没变`;
				break;
			case HAND_RESULT.HAND_1_WIN:
				this.player.coin += betting;
				sm += `你赢了！得到了${betting}硬币！`;
				break;
			case HAND_RESULT.HAND_2_WIN:
				this.player.coin -= betting;
				sm += `你输了！失去了${betting}硬币！`;
				break;
		}
		sm += `\n你现在有${this.player.coin}硬币`;

		return {
			msg: sm
		};
	}

	strToHand(str: string): HAND {
		switch (str) {
			case "石头": return HAND.STONE;
			case "剪刀": return HAND.SCILLORS;
			case "布": return HAND.CLOTH;
		}
		return this.randHand();
	}


	decide(hand1: HAND, hand2: HAND) {
		if (hand1 == hand2) return HAND_RESULT.DRAW;
		if (hand1 == HAND.CLOTH && hand2 == HAND.STONE ||
			hand1 == HAND.SCILLORS && hand2 == HAND.CLOTH ||
			hand1 == HAND.STONE && hand2 == HAND.SCILLORS)
			return HAND_RESULT.HAND_1_WIN;
		return HAND_RESULT.HAND_2_WIN;
	}
}
